﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CustomTextBox.Controls
{
    public class PhoneTextBox : Control
    {
        #region Members

        private TextBlock _hintTextBlock;
        private TextBox _controlTextBox;

        #endregion Members

        #region DependencyProperties

        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(PhoneTextBox), new PropertyMetadata(null));

        public string Hint
        {
            get { return (string)GetValue(HintProperty); }
            set { SetValue(HintProperty, value); }
        }

        public static readonly DependencyProperty HintProperty =
            DependencyProperty.Register("Hint", typeof(string), typeof(PhoneTextBox), new PropertyMetadata(null));

        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(PhoneTextBox), new PropertyMetadata(string.Empty));

        #endregion DependencyProperties

        public PhoneTextBox()
        {
            DefaultStyleKey = typeof(PhoneTextBox);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this._hintTextBlock = (TextBlock)GetTemplateChild("HintTextBlock");
            this._controlTextBox = (TextBox)GetTemplateChild("ControlTextBox");
        }

        protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e)
        {
            base.OnKeyUp(e);
            this._hintTextBlock.Visibility = string.IsNullOrWhiteSpace(this._controlTextBox.Text) ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}